import Vue from 'vue'
//引入vuex
import Vuex from 'vuex'
Vue.use(Vuex)

import { Dialog } from 'vant';
export default new Vuex.Store({
    state: {
        count: 5,
        arr1: JSON.parse(sessionStorage.getItem("list")) || [],
        ids: [],
        user: JSON.parse(sessionStorage.getItem("user")) || [],
        music_info: {},
    },
    mutations: {
        user_info(state, b) {
            state.user = b
            sessionStorage.setItem("user", JSON.stringify(state.user))
        },
        add(state, b) {
            state.arr1.some((item, index, arr) => {
                if (item.id == b.id) {
                    item.num++;
                    sessionStorage.setItem("list", JSON.stringify(state.arr1));
                    return
                }
            })
        },
        raduce(state, b) {
            state.arr1.some((item, index, arr) => {
                if (item.id == b.id) {
                    item.num--;
                    sessionStorage.setItem("list", JSON.stringify(state.arr1));
                    return;
                }
            })

        },
        change(state, b) {
            state.arr1.some((item, index, arr) => {
                if (item.id == b.id) {
                    if (b.num > 200) {
                        item.num = 200;
                    } else {
                        item.num = b.num;
                    }
                    sessionStorage.setItem("list", JSON.stringify(state.arr1));
                    return;
                }
            })
        },
        deleta(state, b) {
            console.log("删除")
            state.arr1.some((item, index, arr) => {
                if (item.id == b.id) {
                    Dialog.confirm({
                            title: '删除吗',
                            message: '确认删除该商品吗',
                        })
                        .then(() => {
                            // on confirm
                            state.arr1.splice(index, 1);

                            sessionStorage.setItem("list", JSON.stringify(state.arr1));
                        })
                        .catch(() => {
                            // on cancel 
                            item.num = 1;
                        });

                }
            })
        },
        arrChange(state, a) {
            var falg = false;
            state.arr1.some((item, index, arr) => {

                if (item.id == a.id) {
                    item.num++;
                    falg = true;
                    return;
                }
            });
            if (!falg) {
                state.arr1.push(a);
            }
            sessionStorage.setItem("list", JSON.stringify(state.arr1));
        },
        audio(state, b) {
            state.music_info = b
        }
    },
    getters: {
        allCount(state) {
            var num = 0;
            state.arr1.forEach((item, index, arr) => {
                num += item.num;
            })
            return num;
        },
        getAllmoney(state) {
            var summoney = 0;
            for (var j = 0; j < state.arr1.length; j++) {
                summoney += state.arr1[j].num * state.arr1[j].gender
            }
            return summoney;
        }
    }
})